package com.lucky.back.service.impl;

import com.lucky.back.ex.ServiceException;
import com.lucky.back.mapper.ProductClassMapper;
import com.lucky.back.pojo.dto.ProductClassAddNewDTO;
import com.lucky.back.pojo.entity.Product;
import com.lucky.back.pojo.entity.Productclass;
import com.lucky.back.pojo.vo.ProductClassListItemVO;
import com.lucky.back.pojo.vo.ProductClassStandardVO;
import com.lucky.back.pojo.vo.ProductStandardVO;
import com.lucky.back.service.IClassService;
import com.lucky.back.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ClassServiceImpl implements IClassService {
    @Autowired
    ProductClassMapper classMapper;

    @Override
    public void updateById(Productclass productclass) {
        log.debug("开始处理【根据id修改小分类详情】的业务，参数：{}", productclass);
        classMapper.updateById(productclass);
    }

    @Override
    public ProductClassStandardVO getStandardById(Long id) {

        log.debug("开始处理【根据id查询品牌详情】的业务，参数：{}", id);
        ProductClassStandardVO productClassStandardVO = classMapper.getStandardById(id);
        if (productClassStandardVO == null) {
            String message = "获取品牌详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return productClassStandardVO;
    }

    @Override
    public void addNew(ProductClassAddNewDTO productClassAddNewDTO) {
        int count = classMapper.countByName(productClassAddNewDTO.getClass1());
        if (count != 0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加失败,名称已被占用");
        }
        Productclass productclass = new Productclass();
        BeanUtils.copyProperties(productClassAddNewDTO,productclass);
        System.out.println("即将执行插入分类service");
        classMapper.insert(productclass);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除请求");
        ProductClassStandardVO queruResult = classMapper.getStandardById(id);
        if (queruResult == null){
            String message = "删除小分类失败,,尝试访问数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将删除分类数据");
        classMapper.deleteById(id);
        log.debug("删除分类，完成！");
    }

    @Override
    public List<ProductClassListItemVO> list() {
        log.debug("查询种类列表服务层——————————————————");
        return classMapper.list();
    }

    @Override
    public List<List<ProductClassListItemVO>> listBySupId(List<Long> idlist) {
        List<List<ProductClassListItemVO>> lists = new ArrayList<>();
        for (Long id : idlist) {
            List<ProductClassListItemVO> list = classMapper.listBySupId(id);
            lists.add(list);
        }

        return lists;

    }
}
